package com.common.android.utils.http;

import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.json.JSONObject;

import android.graphics.Bitmap;

public class HttpCom {
	
//	private static  HttpCom instance = new HttpCom();
	protected HttpBaseClient httpclient;
	protected static String sid;
	protected boolean mIsNewPro = false;

	
	public HttpCom(){
		initHttpClient(false);
	}
	/**
     * 增加一个传参构造，区别于无参构造，获取支持https的client时用
     * Creates a new instance of HttpClient.
     * <br>Created 2014-9-25 上午9:55:36
     * @param httpsable
     */
	public HttpCom(int httpsable){
		initHttpClient(false,httpsable);
	}
	
	/**
	 * 
	 * Creates a new instance of HttpCom.
	 *
	 * @param b  true表示COOKIE内容中用OAPSID
	 *           false表示COOKIE内容中用PHPSESSID
	 *           服务端定义的烂规则
	 */
	public HttpCom(boolean bNewPro){
	    mIsNewPro = bNewPro;

		initHttpClient(bNewPro);
	}
	
	protected void initHttpClient(boolean bNewPro){
		if(bNewPro)
		    httpclient = new HttpClientExt();
		else
			httpclient = new HttpClient();
	}
	/**
	 * 获取支持https请求的client
	 * 
	 * <br>Created 2014-9-25 上午9:59:18
	 * @param bNewPro
	 * @param httpsable
	 * @author       jiaoyun
	 */
	private void initHttpClient(boolean bNewPro,int httpsable){
		if(bNewPro)
		    httpclient = new HttpClientExt(httpsable);
		else
			httpclient = new HttpClient(httpsable);
	}
	
	public Response dopost(String url, byte[] b) throws HttpAuthException,HttpServerException, HttpException{		
		return httpclient.httpRequest(formatUrl(url), b,null, HttpPost.METHOD_NAME);
	}
	
	public Response post(String url, JSONObject json) throws HttpAuthException,HttpServerException, HttpException{		
		return httpclient.httpRequest(formatUrl(url), json, HttpPost.METHOD_NAME);
	}
	
	public Response get(String url ) throws HttpAuthException,HttpServerException, HttpException{
		return httpclient.httpRequest(formatUrl(url), null, HttpGet.METHOD_NAME);
	}
	
	public Response put(String url , JSONObject json ) throws HttpAuthException,HttpServerException, HttpException{
		return httpclient.httpRequest(formatUrl(url), json, HttpPut.METHOD_NAME);
	}

	public Response delete(String url)throws HttpAuthException,HttpServerException, HttpException{
		return httpclient.httpRequest(formatUrl(url), null, HttpDelete.METHOD_NAME);
	}
	
//    public String getSid(){
//        return this.sid;
//    } 
    
	protected String formatUrl(String url){
		
		String urltemp = url;
//		String sid = getSid();
		if(sid==null||"".equals(sid)||url == null) return url;		
		if(urltemp.contains("sid")||urltemp.contains(sid)) return urltemp;
		else{
			if(urltemp.contains("?")){
				urltemp+="&sid="+sid;
			}
			else{
				urltemp+="?sid="+sid;
			}
		}
		return urltemp;
	}
	
	 public Response updateImg(String url, Bitmap f0, String httpMethod) throws HttpException{
		return httpclient.httpUpImageRequest(formatUrl(url), f0, HttpDelete.METHOD_NAME);
	}
}